/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Compras;

import Controlador.Bean.Almacen.Producto;
import Controlador.Bean.Compras.EvaluaProveedor;
import gui.General.Main;
import java.awt.Component;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import lib.Validaciones;
import Atxy2k.CustomTextField.RestrictedTextField;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author LiziTa
 */
public class evaluarProveedor extends javax.swing.JInternalFrame {

    /**
     * Creates new form evaluarProveedor
     */
    
   TableModel tb= new TableModel();
    
    public String prod;
    public String produ="";
    public Double cant=0.0;
 public Double precio=0.0;
 public Double leadtime=0.0;
    public Integer evaluar=0;
        public List<EvaluaProveedor> data2;
    
    public evaluarProveedor() {
       
        initComponents();
        initComp();
//             TableRowSorter<TableModel> sorter  = new TableRowSorter<TableModel>((TableModel) tblProveedores.getModel());
//         tblProveedores.setRowSorter(sorter);
               
         
    }
    
    
    private static evaluarProveedor myoc=null;
     public static evaluarProveedor getInstance() {
         if(myoc == null){
                       myoc = new evaluarProveedor();    
            Main.getDesktop().add(myoc); 
            myoc.setVisible(true);
               myoc.toFront();           
                    }
                    else{
                        if (myoc.isClosed()|| !myoc.isVisible()) {
                                myoc = new evaluarProveedor(); 
                                Main.getDesktop().add(myoc);
                                myoc.toFront();
                                myoc.setVisible(true);
                        }
                        else
                            myoc.toFront();
                    }
        
        
        
                return myoc;
    }
    
   public   List<Producto> productos = Controlador.Service.Almacen.GestionAlmacenes.obtenerProductos();
   
 private void initComp() {
     evaluar=0;
    
      tblProveedores.setModel(tb);
   RestrictedTextField r = new RestrictedTextField(txtPrecio);
   //RestrictedTextField r1 = new RestrictedTextField(txtCantidad);
  RestrictedTextField r2 = new RestrictedTextField(txtTiempo);
  
  r.setLimit(10);
  //r1.setLimit(10);
  r2.setLimit(10);
    }
    
   
 
 /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
 
  //
      public EvaluaProveedor  ep  ;
      public List<EvaluaProveedor> data =new ArrayList<>();; 
 
 class TableModel extends AbstractTableModel{

    int evaluar; 
      String[] columnNames = {"Proveedor","Proveedor", "Precio","Tiempo de entrega","Puntuación"};
  
        public void actualizarTabla(){
            tb.fireTableDataChanged();
           
        }
     
        @Override
        public int getRowCount() {
             return data.size();
        }

        @Override
        public int getColumnCount() {
             return columnNames.length;

        }

        @Override
        public Object getValueAt(int row, int column) {
            
           
           switch(column){
                case 0:return data.get(row).getNombre();
                //case 1: return data.get(row).getRazonsocial(); 
                case 1: return data.get(row).getIdproveedor();
                case 2: return data.get(row).getPreciobase();
                case 3: return data.get(row).getLeadtime();
                default: return data.get(row).getCantidad();
            }
            
        }
         @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
        
     
    }
 
 
    @SuppressWarnings("unchecked")
    
     

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jRadioButton1 = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtTiempo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPrecio = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        btnEvaluar = new javax.swing.JButton();
        javax.swing.JButton btnCancelar = new javax.swing.JButton();
        txtProducto = new javax.swing.JTextField();
        txtIdProducto = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProveedores = new javax.swing.JTable();

        jRadioButton1.setText("jRadioButton1");

        setClosable(true);
        setTitle("Evaluar Proveedores");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Campos a evaluar"));

        jLabel1.setText("Tiempo de entrega <=");

        jLabel3.setText("dias");

        jLabel10.setText("soles");

        jLabel13.setText("Producto");

        btnEvaluar.setText("Evaluar");
        btnEvaluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEvaluarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        txtProducto.setEditable(false);

        txtIdProducto.setEditable(false);
        txtIdProducto.setEnabled(false);

        jLabel2.setText("IdProducto");

        jLabel4.setText(" Precio <=");

        jButton1.setText("Seleccionar Producto");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel13)
                            .addComponent(jLabel2))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtProducto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                                .addComponent(txtIdProducto, javax.swing.GroupLayout.Alignment.LEADING))
                            .addComponent(jButton1))
                        .addGap(61, 61, 61)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtTiempo, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel3)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(207, 207, 207)
                        .addComponent(btnEvaluar)
                        .addGap(27, 27, 27)
                        .addComponent(btnCancelar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIdProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel4))
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(txtProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtTiempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEvaluar)
                    .addComponent(btnCancelar))
                .addGap(20, 20, 20))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultado de Proveedores según mejor calificación"));

        tblProveedores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Razon Social", "Title 2", "Producto", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblProveedores);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(37, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnCancelarActionPerformed

    public boolean valido(){
    boolean b =true;
    String s1="";
     String s2="";
      String s3="";
       String s4="";
    if (txtIdProducto.getText().isEmpty() || txtProducto.getText().isEmpty()){
            b = false;
           s1 ="Debe seleccionar un producto" ;
        }
            
    
    if(!(Validaciones.validarReal(txtTiempo.getText()))&& !txtTiempo.getText().isEmpty()){
            b = false;
           s2 ="Dato no valido para tiempo de entrega" ;
        }
        
       
//    if(!(Validaciones.validarEntero(txtCantidad.getText()))&& !txtCantidad.getText().isEmpty()){
//            b = false;
//           s3 ="Dato no valido para cantidad" ;
//        }
       
    if(!(Validaciones.validarReal(txtPrecio.getText()))&& !txtPrecio.getText().isEmpty()) {
            b = false;
           s4 ="Dato no valido para precio" ;
        }
    
    String rpta="";
         if (s1!="") rpta = rpta+ s1 + "\n";
         if (s2!="") rpta = rpta+ s2 + "\n";
         if (s3!="") rpta = rpta+ s3 + "\n";
          if (s4!="") rpta = rpta+ s3 + "\n";
    
     if (rpta!="")
         JOptionPane.showMessageDialog(null, "Errores: \n" + rpta );
     
    return b;
    }
    
    private void btnEvaluarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEvaluarActionPerformed
        // TODO add your handling code here:
       if (valido()){ evaluar=1;
        try{
       leadtime=Double.parseDouble(txtTiempo.getText());
       }catch(Exception ex){
            leadtime=0.0;
            
        };
        
                    
           try{
       precio=Double.parseDouble(txtPrecio.getText());
       }catch(Exception ex){
            precio=0.0;
            
        };
     
       ep=new EvaluaProveedor("","","", txtProducto.getText(), leadtime,precio, 0.0);
         data = Controlador.Service.Compras.GestionCompra.obtenerResultado(ep,txtProducto.getText()); 
         
         tb.actualizarTabla();
       }
     
    }//GEN-LAST:event_btnEvaluarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        EvaluaProveedorSeleccionarProducto epr= new EvaluaProveedorSeleccionarProducto();
        Main.getDesktop().add(epr);
        epr.setVisible(true);
        
      
      
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEvaluar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblProveedores;
    public static javax.swing.JTextField txtIdProducto;
    private javax.swing.JTextField txtPrecio;
    public static javax.swing.JTextField txtProducto;
    private javax.swing.JTextField txtTiempo;
    // End of variables declaration//GEN-END:variables

   
}
